package com.community.web.controller.community;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.community.body.domain.ZyCommunity;
import com.community.body.dto.ZyCommunityDTO;
import com.community.body.service.IZyCommunityService;
import com.community.body.vo.EchartsVo;
import com.community.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.community.common.annotation.Log;
import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.enums.BusinessType;
import com.community.common.utils.poi.ExcelUtil;
import com.community.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 小区信息Controller
 * 
 * @author community
 * @date 2024-04-09
 */
@RestController
@RequestMapping("/community/village")
public class ZyCommunityController extends BaseController
{
    @Autowired
    private IZyCommunityService zyCommunityService;

    /**
     * 查询buildings列表
     */
    @PreAuthorize("@ss.hasPermi('community:village:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZyCommunityDTO zyCommunity)
    {
        startPage();
        List<ZyCommunity> list = zyCommunityService.selectZyCommunityList(zyCommunity);
        return getDataTable(list);
    }

    @GetMapping("/map")
    public TableDataInfo getMap(ZyCommunity zyCommunity)
    {
        List<EchartsVo> list = zyCommunityService.getMap(zyCommunity);
        for (EchartsVo echartsVo : list) {
            echartsVo.setName(echartsVo.getName().split("省|市|自治区|壮族|特别|回族|维吾尔")[0]);
        }
        return getDataTable(list);
    }

    @GetMapping("/person")
    public TableDataInfo getPersonList(SysUser user)
    {
        List<EchartsVo> list = zyCommunityService.getPersonList(user);
        return getDataTable(list);
    }

    /**
     * 导出buildings列表
     */
    @PreAuthorize("@ss.hasPermi('community:village:export')")
    @Log(title = "buildings", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZyCommunity zyCommunity)
    {
        String[] split = zyCommunity.getCommunityCode().split(",");
        Long[] longArray = Arrays.stream(split).map(Long::valueOf).toArray(Long[]::new);
        List<ZyCommunity> list = zyCommunityService.selectZyCommunityExport(longArray);
        ExcelUtil<ZyCommunity> util = new ExcelUtil<ZyCommunity>(ZyCommunity.class);
        util.exportExcel(response, list, "community数据");
    }

    /**
     * 获取buildings详细信息
     */
    @PreAuthorize("@ss.hasPermi('community:village:query')")
    @GetMapping(value = "/{communityId}")
    public AjaxResult getInfo(@PathVariable("communityId") Long communityId)
    {
        return success(zyCommunityService.selectZyCommunityByCommunityId(communityId));
    }

    /**
     * 新增buildings
     */
    @PreAuthorize("@ss.hasPermi('community:village:add')")
    @Log(title = "buildings", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZyCommunity zyCommunity)
    {
        return toAjax(zyCommunityService.insertZyCommunity(zyCommunity));
    }

    /**
     * 修改buildings
     */
    @PreAuthorize("@ss.hasPermi('community:village:edit')")
    @Log(title = "buildings", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZyCommunity zyCommunity)
    {
        return toAjax(zyCommunityService.updateZyCommunity(zyCommunity));
    }

    /**
     * 删除buildings
     */
    @PreAuthorize("@ss.hasPermi('community:village:remove')")
    @Log(title = "buildings", businessType = BusinessType.DELETE)
	@DeleteMapping("/{communityIds}")
    public AjaxResult remove(@PathVariable Long[] communityIds)
    {
        return toAjax(zyCommunityService.deleteZyCommunityByCommunityIds(communityIds));
    }


    /**
     * 导入buildings
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "社区管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('community:village:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ZyCommunity> util = new ExcelUtil<ZyCommunity>(ZyCommunity.class);
        List<ZyCommunity> communityList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = zyCommunityService.importCommunity(communityList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ZyCommunity> util = new ExcelUtil<ZyCommunity>(ZyCommunity.class);
        util.importTemplateExcel(response, "社区数据");
    }

}
